package cn.bdqn.web.interceptor;

import cn.bdqn.pojo.SysUser;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

public class AuthorizationInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        SysUser user = (SysUser)session.getAttribute("loginUser");
        if (user==null){
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write("<script type='text/javascript'>" +
                    "alert('请先进行登陆，再进行后续操作！(Interceptor控制)');" +
                    "window.location.href='login.jsp';" +
                    "</script>");
            return false;
        }
        return true;
    }
}
